<?php

namespace app\api\controllers;

use yii\web\Controller;
use app\models\GkyQyhApproval;
use app\api\Response;
use yii\db\Query;

class ApprovalController extends Controller {

    public $enableCsrfValidation = false;

    /*
     * 待我审批的
     */

    //根据tableName以及id查找未审批的
    public function selectApproving($tableName, $id) {
        return (new Query())
                        ->select('u.avatar,u.name,l.print_code,l.status,l.create_time')
                        ->from($tableName . ' as l')
                        ->leftJoin('gky_qyh_user as u', 'l.proposer_id=u.id')
                        ->where(['l.approver_id' => $id, 'l.status' => 0])
                        ->all();
    }

    //根据tableName以及id查找已经审批的
    public function selectApproved($tableName, $id) {
        return (new Query())
                        ->select('u.avatar,u.name,l.print_code,l.status,l.agree_time')
                        ->from($tableName . ' as l')
                        ->leftJoin('gky_qyh_user as u', 'l.proposer_id=u.id')
                        ->where(['l.approver_id' => $id, 'l.status' => 1])
                        ->orWhere(['l.approver_id' => $id, 'l.status' => -1])
                        ->all();
    }

    //获取未审批的
    function actionGetApproving($id) {
        $result = array();
        $leave = $this->selectApproving('gky_qyh_sp_leave', $id);
        $result = array_merge($result, $leave);
        $statement = $this->selectApproving('gky_qyh_sp_statement', $id);
        $result = array_merge($result, $statement);
        $goabroad = $this->selectApproving('gky_qyh_sp_goabroad', $id);
        $result = array_merge($result, $goabroad);
        $authorization = $this->selectApproving('gky_qyh_sp_authorization', $id);
        $result = array_merge($result, $authorization);
        $business = $this->selectApproving('gky_qyh_sp_business', $id);
        $result = array_merge($result, $business);
        $this->bubbleSort($result, 'create_time');
        Response::setHeader();
        echo json_encode($result);
    }

    //获取已经审批的
    function actionGetApproved($id) {
        $result = array();
        $leave = $this->selectApproved('gky_qyh_sp_leave', $id);
        $result = array_merge($result, $leave);
        $statement = $this->selectApproved('gky_qyh_sp_statement', $id);
        $result = array_merge($result, $statement);
        $goabroad = $this->selectApproved('gky_qyh_sp_goabroad', $id);
        $result = array_merge($result, $goabroad);
        $authorization = $this->selectApproved('gky_qyh_sp_authorization', $id);
        $result = array_merge($result, $authorization);
        $business = $this->selectApproved('gky_qyh_sp_business', $id);
        $result = array_merge($result, $business);
        $this->bubbleSort($result, 'agree_time');
        Response::setHeader();
        echo json_encode($result);
    }

    function selectSearchApproving($tableName, $id, $name) {
        return (new Query())
                        ->select('u.avatar,u.name,l.print_code,l.status')
                        ->from($tableName . ' as l')
                        ->leftJoin('gky_qyh_user as u', 'l.proposer_id=u.id')
                        ->where(['l.approver_id' => $id, 'l.status' => 0])
                        ->andWhere(['like', 'u.name', $name])
                        ->all();
    }

    function selectSearchApproved($tableName, $id, $name) {
        return (new Query())
                        ->select('u.avatar,u.name,l.print_code,l.status')
                        ->from($tableName . ' as l')
                        ->leftJoin('gky_qyh_user as u', 'l.proposer_id=u.id')
                        ->where(['l.approver_id' => $id, 'l.status' => 0])
                        ->orWhere(['l.approver_id' => $id, 'l.status' => -1])
                        ->andWhere(['like', 'u.name', $name])
                        ->all();
    }

    //搜索待审批
    function actionSearchApproving($id, $name) {
        $result = array();
        $leave = $this->selectSearchApproving('gky_qyh_sp_leave', $id, $name);
        $result = array_merge($result, $leave);
        $statement = $this->selectSearchApproving('gky_qyh_sp_statement', $id, $name);
        $result = array_merge($result, $statement);
        $goabroad = $this->selectSearchApproving('gky_qyh_sp_goabroad', $id, $name);
        $result = array_merge($result, $goabroad);
        $authorization = $this->selectSearchApproving('gky_qyh_sp_authorization', $id, $name);
        $result = array_merge($result, $authorization);
        $business = $this->selectSearchApproving('gky_qyh_sp_business', $id, $name);
        $result = array_merge($result, $business);
        Response::setHeader();
        echo json_encode($result);
    }

    //搜索已经审批
    function actionSearchApproved($id, $name) {
        $result = array();
        $leave = $this->selectSearchApproved('gky_qyh_sp_leave', $id, $name);
        $result = array_merge($result, $leave);
        $statement = $this->selectSearchApproved('gky_qyh_sp_statement', $id, $name);
        $result = array_merge($result, $statement);
        $goabroad = $this->selectSearchApproved('gky_qyh_sp_goabroad', $id, $name);
        $result = array_merge($result, $goabroad);
        $authorization = $this->selectSearchApproved('gky_qyh_sp_authorization', $id, $name);
        $result = array_merge($result, $authorization);
        $business = $this->selectSearchApproved('gky_qyh_sp_business', $id, $name);
        $result = array_merge($result, $business);
        Response::setHeader();
        echo json_encode($result);
    }

    /*
     * 我发起的
     */

    //根据tableName以及id查找未审批的
    public function selectApprovingPropose($tableName, $id) {
        return (new Query())
                        ->select('u.avatar,u.name,l.*')
                        ->from($tableName . ' as l')
                        ->leftJoin('gky_qyh_user as u', 'l.proposer_id=u.id')
                        ->where(['l.proposer_id' => $id, 'l.status' => 0])
                        ->all();
    }

    //根据tableName以及id查找已经审批的
    public function selectApprovedPropose($tableName, $id) {
        return (new Query())
                        ->select('u.avatar,u.name,l.*')
                        ->from($tableName . ' as l')
                        ->leftJoin('gky_qyh_user as u', 'l.proposer_id=u.id')
                        ->where(['l.proposer_id' => $id, 'l.status' => 1])
                        ->orWhere(['l.proposer_id' => $id, 'l.status' => -1])
                        ->all();
    }

    public function actionGetApprovingPropose($id) {
        $result = array();
        $leave = $this->selectApprovingPropose('gky_qyh_sp_leave', $id);
        $result = array_merge($result, $leave);
        $statement = $this->selectApprovingPropose('gky_qyh_sp_statement', $id);
        $result = array_merge($result, $statement);
        $goabroad = $this->selectApprovingPropose('gky_qyh_sp_goabroad', $id);
        $result = array_merge($result, $goabroad);
        $authorization = $this->selectApprovingPropose('gky_qyh_sp_authorization', $id);
        $result = array_merge($result, $authorization);
        $business = $this->selectApprovingPropose('gky_qyh_sp_business', $id);
        $result = array_merge($result, $business);
        $result = $this->bubbleSort($result, 'create_time');
        Response::setHeader();
        echo json_encode($result);
    }

    public function actionGetApprovedPropose($id) {
        $result = array();
        $leave = $this->selectApprovedPropose('gky_qyh_sp_leave', $id);
        $result = array_merge($result, $leave);
        $statement = $this->selectApprovedPropose('gky_qyh_sp_statement', $id);
        $result = array_merge($result, $statement);
        $goabroad = $this->selectApprovedPropose('gky_qyh_sp_goabroad', $id);
        $result = array_merge($result, $goabroad);
        $authorizarion = $this->selectApprovedPropose('gky_qyh_sp_authorization', $id);
        $result = array_merge($result, $authorizarion);
        $business = $this->selectApprovedPropose('gky_qyh_sp_business', $id);
        $result = array_merge($result, $business);
        $result = $this->bubbleSort($result, 'agree_time');
        Response::setHeader();
        echo json_encode($result);
    }
    
    public function bubbleSort($arr,$str){
        $len = count($arr);
        for ($i = 1; $i < $len; $i++) { 
            for ($k = 0; $k < $len - $i; $k++) {
                if ($arr[$k][$str] < $arr[$k + 1][$str]) {
                    $tmp = $arr[$k + 1];
                    $arr[$k + 1] = $arr[$k];
                    $arr[$k] = $tmp;
                }
            }
        }
        
        return $arr;
    }
    
}
